package com.lyz.dataStructure.algorithm.美团2020;/**
 * @Author:759057893@qq.com Lyz
 * @Date: ${Date} 17:23
 * @Description:
 **/

/**
 *@Author:759057893@qq.com Lyz
 *@Date: 2019/8/23 17:23
 *@Description:
 **/
import java.util.*;
public class Main5 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        String s = sc.nextLine();
        String[] st = s.split(",");
        int[] nums = new int[st.length];
        for(int i =0;i<nums.length;i++){
            nums[i] = Integer.parseInt(st[i]);
        }
        int m =sc.nextInt();
        //System.out.println(2);
        System.out.println(splitArray(nums,m));

    }
   /* public static int splitArray(int[] nums, int m) {
        long left =0;
        long right = 0;
        int length = nums.length;
        for(int i =0;i<length;i++){
            right = right+nums[i];
            if(left<nums[i]){
                left = nums[i];
            }
        }
        long n = right;
        while(left<=right){
            long mid = (left+right)>>1;
            long n1 = 1;
            int sum = 0;
            for(int i =0;i<n;i++){
                if(sum+nums[i]>mid){
                    n1++;
                    sum = nums[i];
                }else{
                    sum+=nums[i];
                }
            }
            if(n1<=m){
                n =Math.min(n,mid);
                right = mid -1;
            }else{
                left = mid +1;
            }
        }
        return (int)n;

    }*/
   public static int splitArray(int[] nums, int m) {
       long l = 0;
       long r = 0;
       int n = nums.length;
       for (int i = 0; i < n; i++) {
           r += nums[i];
           if (l < nums[i]) {
               l = nums[i];
           }
       }
       long ans = r;
       while (l <= r) {
           long mid = (l + r) >> 1;
           long sum = 0; int cnt = 1;
           for (int i = 0; i < n; i++) {
               if (sum + nums[i] > mid) {
                   cnt ++; sum = nums[i];
               } else { sum += nums[i];
               }
           } if (cnt <= m) {
               ans = Math.min(ans, mid);
               r = mid - 1;
           } else {
               l = mid + 1;
           }
       } return (int)ans;
   }

}
